Skip to content

gh-127081: lock non-re-entrant *pwent calls#132748

Merged
kumaraditya303 merged 6 commits into
python:mainfrom
duaneg:gh-127081-pwd
May 22, 2025
Merged

gh-127081: lock non-re-entrant *pwent calls#132748
kumaraditya303 merged 6 commits into
python:mainfrom
duaneg:gh-127081-pwd

Conversation

@duaneg

@duaneg duaneg commented Apr 20, 2025

Copy link
Copy Markdown
Contributor

The libc setpwent, getpwent, and endpwent functions are not thread-safe. Protect them with mutexs in free-threading builds.

The libc setpwent, getpwent, and endpwent functions are not thread-safe.
Protect them with mutexs in free-threading builds.
@picnixz picnixz changed the title gh-127081: lock non-re-entrant *pwent calls gh-127081: lock non-re-entrant *pwent calls Apr 20, 2025

@ZeroIntensity ZeroIntensity left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need a critical section here. Py_CLEAR and Py_DECREF are re-entrant (as in, they can invoke the eval loop), so a mutex is prone to lock-ordering or re-entrancy deadlocks.

@ZeroIntensity

Copy link
Copy Markdown
Member

Actually, sorry, we need to fix subinterpreter thread-safety here too. We can't do that with a critical section. Could you adjust the mutex to go over solely the libc calls?

Comment thread Modules/pwdmodule.c
Comment thread Modules/pwdmodule.c Outdated
@duaneg

duaneg commented Apr 20, 2025

Copy link
Copy Markdown
Contributor Author

Py_CLEAR and Py_DECREF are re-entrant...Could you adjust the mutex to go over solely the libc calls?

Hmm, yeah, good point. I guess we can just defer the decrefs until exit.

Comment thread Modules/pwdmodule.c Outdated
Comment thread Modules/pwdmodule.c Outdated
Comment thread Modules/pwdmodule.c Outdated
@kumaraditya303
kumaraditya303 self-requested a review May 21, 2025 16:51
Comment thread Modules/pwdmodule.c
… they are

strongly referenced by that, so we can dec-ref them safely.
Comment thread Modules/pwdmodule.c Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants